package com.ark.socket.service;

import com.ark.service.service.GameDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Component
@ServerEndpoint("/ws")
public class WebSocketService {

    private static final Map<String, Session> CLIENTS = new ConcurrentHashMap<>();

    @Resource
    private GameDeviceService gameDeviceService;

    @OnOpen
    public void onOpen(Session session){
        /*if (gameDeviceService.isDeviceExist("")) {

        }*/
        log.info("用户【sessionId={}】建立连接！", session.getId());
    }

    @OnClose
    public void onClose(Session session) {
        log.info("用户【sessionId={}】关闭连接！", session.getId());
    }

    @OnError
    public void onError(Session session, Throwable exception){
        log.error("用户【sessionId={}】发生错误！", session.getId(), exception);
    }

    @OnMessage(maxMessageSize = 5242880)
    public void onMessage(Session session, String message) {
        log.warn("用户【sessionId={}】发送消息：{}", session.getId(), message);
    }
}
